package com.mach.platform.security;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import lombok.Cleanup;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author Anonymous
 * @Description:   登录成功处理类----默认 是 SimpleUrlAuthenticationFailureHandler
 * @Package: com.mach.platform.security
 * @Time create on 2017/11/26 21:32
 */

public class CuAntuSuccessHandler implements AuthenticationSuccessHandler{

	/**
	 * Called when a user has been successfully authenticated.
	 *
	 * @param request        the request which caused the successful authentication
	 * @param response       the response
	 * @param authentication the <tt>Authentication</tt> object which was created during
	 */
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
		response.setContentType("application/json;charset=utf-8");
		@Cleanup PrintWriter out = response.getWriter();
		Map map = Maps.newHashMap();
		map.put("status", HttpStatus.OK.value());
		map.put("msg", "登录成功！");
		map.put("info", authentication.getName());
		out.write(JSON.toJSON(map).toString());

	}
}
